<?php
namespace App\Http\Controllers\sender;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\Order_info;
use App\Models\Order_time;
use App\Models\Sender;
use Illuminate\Http\Request;

/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2016-12-26
 * Time: 0:16
 */
class SenderController extends Controller
{
    public function index()
    {
        return view('sender.index');
    }

    public function login()
    {
        return view('sender.login');
    }

    public function dologin(Request $request)
    {
        $ob = new Sender();
        $res = $ob->checkLogin($request);
        if ($res) {
            session(['sender' => $res]);
            return json_encode(true);
        } else {
            return json_encode("账号密码错误请重新输入");
        }
    }

    //封装订单数据接口
    public function redata()
    {
        $id = session('sender')->id;
        $res = Order::
        select("order.*", "order_time.pay_at")
            ->join('order_time', 'order.id', '=', 'order_time.order_id')
            ->where([['sender_id', $id], ['status', 2], ['state', 4]])
            ->orwhere([['sender_id', $id], ['status', 3], ['state', 4]])
            ->get();
        $json1 = '';
        $json = '';
        foreach ($res as $v) {
            $list = Order_info::where('order_id', $v->id)->get();
            $json .= ',"order":' . json_encode($v) . ',' . '"order_info":' . json_encode($list);
            $json1 .= ",{" . ltrim($json, ',') . "}";
        }
        $json = "[" . ltrim($json1, ',') . "]";
        return $json;
    }
    /** 配送员操作，改变订单状态 */
    public function chstu(Request $request)
    {
        $action = $request->input('action');
        $id = $request->input('id');
        $time = date('Y-m-d H:i:s');
        if ($action == 2) {
            $res = Order::where('id', $id)->update(['status' => '3']);
            $ress = Order_time::where('order_id', $id)->update(['sender_at' => $time]);
            return $res + $ress;
        } else {
            $res = Order::where('id', $id)->update(['status' => '4', 'state' => '5']);
            $ress = Order_time::where('order_id', $id)->update(['complete_at' => $time]);
            return ($res + $ress) * 2;
        }
    }

    /** 查询配送员相关信息并渲染视图 */
    public function info()
    {
        $id = session('sender')->id;
        $u=Sender::where('id',$id)->first();
        $list_w = Order::where('sender_id', $id)->where([['status', '2'], ['state', '4']])->get()->count();
        $list_i = Order::where('sender_id', $id)->where([['status', '3'], ['state', '4']])->get()->count();
        $list_c = Order::where('sender_id', $id)->where([['status', '4'], ['state', '5']])->get()->count();
        return view('sender.sender', ['w' => $list_w, 'i' => $list_i, 'c' => $list_c,'u'=>$u]);
    }

    /** 查询配送员各状态订单信息并渲染对应视图视图 */
    public function checkOrder(Request $request)
    {
        $a = $request->input('a');
        $id = session('sender')->id;
        switch ($a) {
            //未配送
            case "w":
                $w = Order::where('sender_id', $id)->where([['status', '2'], ['state', '4']])->get();
                foreach ($w as $v){
                    $v->order_info=$v->find($v->id)->order_info;
                }
                return view('sender.orderinfo',['list'=>$w,'a'=>$a]);
                break;
            //配送中
            case'i':
                $i = Order::where('sender_id', $id)->where([['status', '3'], ['state', '4']])->get();
                foreach ($i as $v){
                    $v->order_info=$v->find($v->id)->order_info;
                }
                return view('sender.orderinfo',['list'=>$i,'a'=>$a]);
                break;
            //已完成
            case'c':
                $c = Order::where('sender_id', $id)->where([['status', '4'], ['state', '5']])->get();
                foreach ($c as $v){
                    $v->order_info=$v->find($v->id)->order_info;
                }
                return view('sender.orderinfo',['list'=>$c,'a'=>$a]);
                break;
        }
    }
    /**　渲染配送员信息修改视图 **/
    public function sendinfo()
    {
        $id=session('sender')->id;
        $list=Sender::where('id',$id)->first();
        return view('sender.sendinfo',['list'=>$list]);
    }

    public function upinfo(Request $request)
    {
        $od = new Sender();
        $id = session('sender')->id;
        $m = $od->upinfo($request, $id);
        return back()->with('msg','个人信息修改成功');
    }
}